package com.product.system.controller;

import com.product.system.imageCline.factory.PicParseStrategyFactory;
import com.product.system.imageCline.strategy.ParsePicStrategy;
import io.minio.MinioClient;
import net.sourceforge.tess4j.TesseractException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.product.system.imageCline.template.PicParseTemplate.*;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@RestController
public class ImageController {

//    @RequestMapping("/images")
//    public String images() throws TesseractException {
//        String path = "C:\\Users\\lenovo\\Pictures\\Saved Pictures\\jnkknnk.jpg";
//        String lagn = "E:\\OCR\\tessdata";
//        File image = new File(path);
//        Tesseract tesseract = new Tesseract();
//        tesseract.setDatapath(path);
//        tesseract.setLanguage("chi_sim");
////        tesseract.setPageSegMode(1);
////        tesseract.setOcrEngineMode(1);
//        String result = tesseract.doOCR(image);
////
//        return result;
//    }
//    public static final String IMAGE_PATH = "C:\\Users\\lenovo\\Pictures\\Saved Pictures\\666.jpg";
    public static final String IMAGE_LAGNGUAGE_PATH = "E:\\OCR\\tessdata";


    @RequestMapping("/images")
    public Map<String,String> images(String imageType,String file) throws TesseractException, IOException {
//        BufferedImage image = ImageIO.read(file);
//        File image = new File();



//        try {
//            URL url = new URL(file);
//            System.out.println(url+"ppppppppppppppppppppp");
//            BufferedImage image = ImageIO.read(url);
//            System.out.println(image);
//            Tesseract tesseract = new Tesseract();
//            tesseract.setDatapath(IMAGE_LAGNGUAGE_PATH);
//            tesseract.setLanguage("chi_sim");
//            String result = tesseract.doOCR(image).replace("\n", "").replace(" ", "");
//            System.out.println(result);
//            int startIndex = result.indexOf("名称") + "名称".length();
//            int endIndex = result.indexOf("注册资本");
//            int startpeople = result.indexOf("法定代表人") + "法定代表人".length();
//            int endpeople = result.indexOf("营业期限");
//            String name = result.substring(startIndex, endIndex).trim();
//            System.out.println("提取的名称："+name);
//            String people = result.substring(startpeople, endpeople).trim();
//            System.out.println("提取的法定人："+people);
//            HashMap<String, String> map = new HashMap<>();
//            map.put("name",name);
//            map.put("people",people);
//            return map;
//        }catch (IOException | TesseractException e){
//            e.printStackTrace();
//            return null;
//        }

        Map<String, String> map = new HashMap<>();
        if (imageType.equals("businessLicense")){
            ParsePicStrategy businessLicense = PicParseStrategyFactory.createStrategy(imageType,file);
            map = businessLicense.parsePicSave(file);
        }else if (imageType.equals("idCard")){
            ParsePicStrategy idCard = PicParseStrategyFactory.createStrategy(imageType,file);
            map = idCard.parsePicSave(file);
        }else if (imageType.equals("driverLicense")){
            ParsePicStrategy driverLicense = PicParseStrategyFactory.createStrategy(imageType,file);
            map = driverLicense.parsePicSave(file);
        }
        return map;
    }
}
